package com.imooc.miaosha.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {

    @Autowired
    JedisPool jedisPool;


    public  <T> T get(KeyPrefix prefix, String key,Class clz){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            String str = jedis.get(prefix.getPrefix() + ":" + key);
            T t = (T) stringToBean(str, clz);
            return  t;
        }
        finally {
            returnToPool(jedis);
        }

    }

    public <T> boolean set(KeyPrefix prefix, String key, T value){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            String str = beanToString(value);
            String realKey = prefix.getPrefix() + ":" + key;
            // 设置是否要过期
            if(prefix.expireSeconds() <= 0){
                jedis.set(realKey, str);

            }else{
                jedis.setex(realKey,  prefix.expireSeconds(), str);
            }
            return true;
        }
        finally {
            // 释放到线程池里面
            returnToPool(jedis);
        }
    }

    public <T> Long incr(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            String realKey = prefix.getPrefix() + ":" + key;
            return  jedis.incr(realKey);
        }
        finally {
            // 释放到线程池里面
            returnToPool(jedis);
        }
    }

    public <T> Long decr(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            String realKey = prefix.getPrefix() + ":" + key;
            return  jedis.decr(realKey);
        }
        finally {
            // 释放到线程池里面
            returnToPool(jedis);
        }
    }

    /**
     *  判断键值是否存在
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> boolean exists(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            return jedis.exists(prefix.getPrefix() + ":" + key);
        }
        finally {
            // 释放到线程池里面
            returnToPool(jedis);
        }

    }

    public <T> boolean delete(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            return jedis.del(prefix.getPrefix() + ":" + key) ==1;
        }
        finally {
            // 释放到线程池里面
            returnToPool(jedis);
        }

    }

    /**
     *  Bean 转化成为 String
     * @param value
     * @param <T>
     * @return
     */
    public  <T> String beanToString(T value) {
        if (value == null){
            return  null;
        }
        Class clazz = value.getClass();
        if(clazz == int.class || clazz == Integer.class) {
            return  "" + value;
        }else if(clazz == String.class){
            return (String) value;
        }else if(clazz == long.class || clazz == Long.class){
            return  "" + value;
        }else{
            return JSON.toJSONString(value);
        }

    }

    /**
     *  String 转成Bean
     * @param <T>
     * @param str
     * @param clazz
     * @return
     */
    public  <T> T stringToBean(String str, Class<T> clazz) {
        if(str == null || str.length() <= 0 || clazz == null ){
            return  null;
        }
        if(clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(str);
        }else if(clazz == String.class){
            return (T) str;
        }else if(clazz == long.class || clazz == Long.class){
            return (T) Long.valueOf(str);
        }else{
            return JSON.toJavaObject(JSON.parseObject(str), clazz);
        }
    }

    private void returnToPool(Jedis jedis) {
        if(jedis != null ){
            jedis.close();
        }
    }


}
